<template>
  <el-card 
    class="feature-intro-card" 
    v-if="visible" 
    :class="{ 'dark-mode': isDark }"
  >
    <template #header>
      <div class="intro-header">
        <el-icon>
          <component :is="headerIcon" />
        </el-icon>
        <span>{{ title }}</span>
        <el-button 
          type="text" 
          class="close-btn" 
          @click="handleClose"
          :icon="Close"
        />
      </div>
    </template>
    <div class="intro-content">
      <p class="intro-description">{{ description }}</p>
      <div class="feature-grid">
        <div 
          v-for="(feature, index) in features" 
          :key="index"
          class="feature-item"
        >
          <el-icon 
            class="feature-icon" 
            :color="feature.color"
          >
            <component :is="feature.icon" />
          </el-icon>
          <h4>{{ feature.title }}</h4>
          <p>{{ feature.description }}</p>
        </div>
      </div>
    </div>
  </el-card>
</template>

<script>
import { ref, computed, onMounted } from 'vue'
import { Close } from '@element-plus/icons-vue'

export default {
  name: 'FeatureIntro',
  components: {
    Close
  },
  props: {
    // 标题
    title: {
      type: String,
      required: true
    },
    // 描述
    description: {
      type: String,
      required: true
    },
    // 头部图标
    headerIcon: {
      type: [String, Object],
      required: true
    },
    // 功能列表
    features: {
      type: Array,
      required: true,
      validator: (features) => {
        return features.every(feature => 
          feature.title && 
          feature.description && 
          feature.icon && 
          feature.color
        )
      }
    },
    // 初始显示状态
    initialVisible: {
      type: Boolean,
      default: true
    }
  },
  emits: ['close'],
  setup(props, { emit }) {
    const visible = ref(props.initialVisible)
    
    // 检测深色模式
    const isDark = computed(() => {
      return document.documentElement.classList.contains('dark')
    })

    // 监听主题变化
    const themeObserver = ref(null)

    onMounted(() => {
      // 创建 MutationObserver 来监听主题变化
      themeObserver.value = new MutationObserver(() => {
        // 触发响应式更新
        isDark.value = document.documentElement.classList.contains('dark')
      })

      themeObserver.value.observe(document.documentElement, {
        attributes: true,
        attributeFilter: ['class']
      })
    })

    const handleClose = () => {
      visible.value = false
      emit('close')
    }

    return {
      visible,
      isDark,
      handleClose
    }
  }
}
</script>

<style scoped>
.feature-intro-card {
  max-width: 90%;
  margin: 30px auto 0 auto;
  background: var(--el-bg-color-overlay, #fafbfc);
  border: 1px solid var(--el-border-color-light, #e4e7ed);
  border-radius: 8px;
  overflow: hidden;
  transition: background-color 0.3s ease, border-color 0.3s ease;
}

.intro-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 500;
  color: var(--el-text-color-regular, #606266);
}

.intro-header .el-icon {
  font-size: 18px;
  color: var(--el-text-color-secondary, #909399);
}

.close-btn {
  margin-left: auto;
  color: var(--el-text-color-secondary, #909399) !important;
  transition: all 0.3s ease;
}

.close-btn:hover {
  background-color: var(--el-fill-color-light, rgba(144, 147, 153, 0.1)) !important;
  color: var(--el-text-color-regular, #606266) !important;
}

.intro-content {
  padding: 0;
}

.intro-description {
  font-size: 14px;
  line-height: 1.6;
  margin-bottom: 20px;
  color: var(--el-text-color-regular, #606266);
  text-align: center;
}

.feature-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
}

.feature-item {
  background: var(--el-bg-color, white);
  padding: 16px;
  border-radius: 6px;
  text-align: center;
  border: 1px solid var(--el-border-color-light, #e4e7ed);
  transition: transform 0.3s ease, box-shadow 0.3s ease, background-color 0.3s ease, border-color 0.3s ease;
}

.feature-item:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px var(--el-box-shadow-light, rgba(0, 0, 0, 0.1));
}

.feature-icon {
  font-size: 24px;
  margin-bottom: 8px;
}

.feature-item h4 {
  margin: 0 0 6px 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--el-text-color-primary, #303133);
}

.feature-item p {
  margin: 0;
  font-size: 12px;
  line-height: 1.4;
  color: var(--el-text-color-secondary, #909399);
}

/* 深色模式适配 */
.feature-intro-card.dark-mode {
  background: var(--el-bg-color-overlay, #1d1e1f);
  border-color: var(--el-border-color, #4c4d4f);
}

.dark-mode .feature-item {
  background: var(--el-fill-color-light, #262727);
  border-color: var(--el-border-color-light, #363637);
}

.dark-mode .feature-item:hover {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .feature-intro-card {
    margin: 20px 16px 0 16px;
    max-width: none;
  }
  
  .feature-grid {
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 12px;
  }
  
  .feature-item {
    padding: 12px;
  }
  
  .feature-icon {
    font-size: 20px;
  }
  
  .feature-item h4 {
    font-size: 13px;
  }
  
  .feature-item p {
    font-size: 11px;
  }
}

@media (max-width: 480px) {
  .feature-grid {
    grid-template-columns: 1fr;
  }
}
</style> 